mini.parse();
var projectName = mini.get("projectName").getValue();
var taskKey = mini.get("taskKey").getValue();
var shopGrid = mini.get("shop_list_datagrid");
var eqGrid = mini.get("equipment_list_datagrid");
var progGrid = mini.get("progress_equipment_list_datagrid");
var checkProgGrid = mini.get("check_progress_equipment_list_datagrid");
var handover_type = mini.get("handover_type");
var accept_people_id = mini.get("accept_people_id");
var check_people_id = mini.get("check_people_id");
var org_id = mini.get("org_id");

function beforeSend(){
	if(taskKey == 'start' || taskKey == 'usertask1'){
		eqGrid.clearRows();
		queryCheckPeople();
		if(!checkAcceptPeople()) return false;
		if(!checkShopGrid()) return false;
		if(!checkCheckPeople()) return false;
	}
	if(taskKey == 'usertask2'){}
	if(taskKey == 'usertask3'){}
	if(taskKey == 'usertask4'){}
	if(taskKey == 'usertask5'){}
	eqGrid.clearRows();
	return true;
}

//预览差异
function viewDiff(){
	if(taskKey == 'usertask3'||taskKey == 'usertask4'||!taskKey){
		var wono = mini.get("wo_no").getValue();
		var ids = '';
		var da = shopGrid.getData();
		for (var i = 0; i < da.length; i++) {
			if(ids.length == 0) ids = da[0].SHOP_ID;
			else ids += ',' + da[0].SHOP_ID;
		}
		var param = '';
		var d = progGrid.getData();
		for (var i = 0; i < d.length; i++) {
			if(param.length == 0) param = d[0].EQ_CODE;
			else param += ',' + d[0].EQ_CODE;
		}
		if(ids){
			mini.open({
				url: projectName+"/CrDetailAdmin/hangOverListPage",
				title: '差异预览', width: 700, height: 500,
				onload: function () {
					var iframe = this.getIFrameEl();
					var data = {checkNo: wono,ids:ids,param:param};
					iframe.contentWindow.setData(data);
				}
			});
		}else mini.alert("没有查询到交接店铺");
	}else mini.alert("该流程尚未生成差异单");
}

//新增在途设备验收清单
function addCheckProgressEquipmentList(){
	var wono = mini.get("wo_no").getValue();
	mini.open({
		  url: projectName+"/PurBarcodeAdmin/EditPage",
		  title: '扫码验收', width: 700, height: 500,
		  onload: function () {
		      var iframe = this.getIFrameEl();
		      var data = {businessKey:wono,statusNumId:'1'};
		      iframe.contentWindow.setData(data);
		  },
		  ondestroy : function(action) {
			  loadCheckProgressEquipmentList();
	      }
	});
}

//加载交接录入的条码
function loadCheckProgressEquipmentList(){
	var wono = mini.get("wo_no").getValue();
	$.ajax({
		url: projectName + '/PurBarcodeAdmin/findPurBarcode',
		data:{businessKey: wono,statusNumId:'1'},
		dataType : 'json',
		async : false,
		success : function(data) {
			var rows = [];
			$.each( data, function(i, o){
				var row = {EQ_CODE : o.equipmentBarcode,EQ_NAME:o.equipmentName,EQ_TYPE_NAME:o.equipmentTypeName};
				rows.push(row);
			})
			checkProgGrid.addRows(rows); 
		},
		error : function() {
			mini.alert("出错啦！");
		}
	})
}

//新增在途设备清单
function addProgressEquipmentList(){
	progGrid.clearRows();
	var createrId = mini.get("creater_id").getValue();
	if(createrId){
		$.ajax({
			url: projectName + '/EqRecipientsAdmin/queryByPeopleID?receivePeopleId=' + createrId + '&isDel=0',
			dataType : 'json',
			async : false,
			success : function(data) {
				var rows = [];
				$.each( data, function(i, o){
					var row = {EQ_CODE : o.equipmentBarcode};
					rows.push(row);
				})
				progGrid.addRows(rows); 
			},
			error : function() {
				mini.alert("出错啦！");
			}
		})
	}else mini.alert("查询交接人出错！");
}

//新增店铺设备清单
function addShopEquipmentList(){
	var rows = shopGrid.getSelecteds();
	if(rows.length != 1){
		mini.alert("请选择且只能选择一家交接店铺");
		return;
	}else{
		var wono = mini.get("wo_no").getValue();
		var shopId = rows[0].SHOP_ID;
		queryInstalledEquipment(rows[0]._uid);
		mini.open({
			url: projectName+"/PurBarcodeAdmin/serviceHandoverAddSearchBar",
			title: '扫码验收', width: 700, height: 500,
			onload: function () {
			    var iframe = this.getIFrameEl();
			    var data = {businessKey:wono,storageNumId:shopId};
			    iframe.contentWindow.setData(data);
			},
			ondestroy : function(action) {
				queryInstalledEquipment(rows[0]._uid);
		    }
		});
	}
}

//扫码验收 
function scanBarCodeCheck(uid){
	try {
		if(taskKey != 'usertask3'){
			mini.alert("请在验收流程进行扫码操作！");
			return;
		}
		addShopEquipmentList();
	} catch (e) {
		mini.alert("出错啦！");
	}
}

//查看店铺安装设备
function queryInstalledEquipment(uid){
	eqGrid.clearRows();
	var shopId = shopGrid.getRowByUID(uid).SHOP_ID;
	var wono = mini.get("wo_no").getValue();
	if(shopId){
		$.ajax({
			url: projectName + '/InventoryShopStkAdmin/queryEqByShopIdForServiceHandover?shopId=' + shopId + '&businessKey='+wono,
			dataType : 'json',
			async : false,
			success : function(data) {
				var rows = [];
				$.each( data, function(i, o){
					if(o.qty > 0){
						var row = {
								EQ_ID : o.equipmentId,
								EQ_NAME : o.equipmentName,
								EQ_TYPE_ID : o.equipmentTypeId,
								EQ_TYPE_NAME : o.equipmentTypeName,
								EQ_NUM : o.qty,
								EQ_NUM_ : o.unConfirqty
						};
						rows.push(row);
					}
				})
				eqGrid.addRows(rows); 
				mini.get("shopId_temp").setValue(shopId);
			},
			error : function() {
				mini.alert("出错啦！");
			}
		})
	}
}

//编辑设备条码
function editBarCode(uid){
	if(taskKey == "usertask3"){
		var c = eqGrid.getRowByUID(uid);
		var businessKey = mini.get("wo_no").getValue();
		var shopId = mini.get("shopId_temp").getValue();
		var eqId=c.EQ_ID;
		mini.open({
			url: projectName+"/PurBarcodeAdmin/serviceHandoverAddSearchBar",
			title: '编辑条码', width: 700, height: 500,
			onload: function () {
				var iframe = this.getIFrameEl();
				var data = {businessKey:businessKey,storageNumId:shopId,eqId:eqId};
				iframe.contentWindow.setData(data);
			},
			ondestroy : function(action) {
				var rows = shopGrid.getSelecteds();
				queryInstalledEquipment(rows[0]._uid);
		    }
		});
	}else mini.alert("此流程禁止编辑条码！");
}

//查看店铺安装设备条码
function queryBarCode(uid){
	var businessKey = mini.get("wo_no").getValue();
	var c = eqGrid.getRowByUID(uid);
	var eqId = c.EQ_ID;
	var shopId = mini.get("shopId_temp").getValue();
	if(taskKey == 'start' || taskKey == 'usertask1' || taskKey == 'usertask2'){
		mini.open({
			url: projectName+"/PurBarcodeAdmin/barCodeByShopIdAndEqId",
			title: '查看条码', width: 700, height: 500,
			onload: function () {
				var iframe = this.getIFrameEl();
				var data = {equipmentId:eqId,storageNumId:shopId};
				iframe.contentWindow.setData(data);
			}
		});
	}else{
		mini.open({
	        url: projectName+"/PurBarcodeAdmin/barCodeEqIDView",
	        title: '查看条码', width: 700, height: 500,
	        onload: function () {
	            var iframe = this.getIFrameEl();
	            var data = {businessKey:businessKey,storageNumId:shopId,eqId:eqId};
	            iframe.contentWindow.setData(data);
	        }
	    });
	}
}

//校验交接店铺清单
function checkShopGrid(){
	var data = shopGrid.getData();
	if (data.length > 0)  return true;
	mini.alert("请添加交接店铺信息");
	return false;
}

//校验审核人  客服经理
function checkCheckPeople(){
	var checkPeopleId = check_people_id.getValue();
	if(!checkPeopleId){
		mini.alert("系统中没有配置处理交接单审核的工作流人员，禁止提交！");
		return false;
	}
	return true;
}

//校验接收人  不能与交接人相同
function checkAcceptPeople(){
	var acceptPeopleId = accept_people_id.getValue();
	var createrId = mini.get("creater_id").getValue();
	if(acceptPeopleId == createrId){
		mini.alert("接收人不能与交接人相同！");
		return false;
	}
	return true;
}

//查询审核人并赋值
function queryCheckPeople(){
	var orgId = org_id.getValue();
	if(orgId){
		$.ajax({
			url : projectName + "/RoleUserAdmin/queryServiceManager?orgId=" + orgId,
			dataType : 'json',
			async : false,
			success : function(data) {
				if (data.status == '1') {
					mini.get("check_people_id").setValue(data.message);
				} else {
					mini.alert("查询交接单审核人员出错!");
				}
			},
			error : function() {
				mini.alert("出错啦!");
			}
		});
	}
}


//新增拆屏店铺
function addShopList(){
	if(velidateform2()){
		shopGrid.clearRows();
		var createrId = mini.get("creater_id").getValue();
		$.ajax({
			url: projectName + '/ShShopAdmin/findShShopByUsr?engineeringStaffId=' + createrId + '&isDel=0&type=0',
			dataType : 'json',
			async : false,
			success : function(data) {
				if(data.length>0){
					var rows = [];
					$.each( data, function(i, o){
						var row = {
								SHOP_ID : o.shopNumId,
								SHOP_AS_ID : o.shopAsId,
								SHOP_NAME : o.shopName,
								SHOP_ADDR : o.contactAddress,
								SHOP_AREA : o.area
						};
						rows.push(row);
					})
					shopGrid.addRows(rows); 
				}else{
					mini.alert("当前登陆人名下不存在管辖的店铺！");
				}
			},
			error : function() {
				mini.alert("出错啦！");
			}
		})
	}
}

//校验必填项
function velidateform2() {
	var form_temp = new mini.Form("form2");
	form_temp.validate();
	if (form_temp.isValid() == false) {
		return false;
	}
	return true;
}

//删除交接店铺
function delShopRow(e){
	var rows = shopGrid.getSelecteds();
	if(rows.length < 1){
		mini.alert("请至少选择一条记录！");
		return;
	}
	var handoverType = handover_type.getValue();
	if(handoverType == '1'){
		mini.alert("全部交接，禁止删除店铺");
		return;
	}
	for(var i=0; i<rows.length;i++){
		shopGrid.removeRow(rows[i]);
	}
}

//交接类型改变   清空店铺子表
function onTypeChange(e){
	var data = shopGrid.getData();
	shopGrid.removeRows(data);
	eqGrid.clearRows();
	progGrid.clearRows();
}

//审批结果如果为不通过,则必须填写审批意见
function onSelectResult1(e){
	onSelectResult(e,"check_memo")
}

function onSelectResult2(e){
	onSelectResult(e,"confirm_memo")
}

function onSelectResult(e,fieldName){
	var re = e.value;
	var c = mini.get(fieldName);
	if( re && re == '1'){
		c.setRequired(false);
		c.setEmptyText("");
		if (e.setIsValid) e.setIsValid(true);
	}
	if( re && re == '0'){
		mini.get(fieldName).setRequired(true);
		c.setEmptyText("请输入审批不通过的原因");
	}
}

function delRow_prog(){
	var handoverType = handover_type.getValue();
	if(handoverType == '1'){
		mini.alert("全部交接，禁止删除在途设备");
		return;
	}
	var rows = progGrid.getSelecteds();
	if(rows.length < 1){
		mini.alert("请至少选择一条记录！");
		return;
	}
	for(var i=0; i<rows.length;i++){
		progGrid.removeRow(rows[i]);
	}
}